package com.baizhi.config;

import com.baizhi.realm.AuthenRealm;
import com.baizhi.realm.AuthorRealm;
import com.baizhi.realm.MyRealm;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * shiro配置类，用于和SpringBoot继承
 *
 * 1、创建过滤工厂，只需要自己配置，不用创建，框架做好了
 * 2、需要注入参数所以要创建安全管理器
 * 3、要注入参数，创建自定义的Realm
 */

@Configuration
public class MyShiroConfig {

//    LoggerFactory
    Logger logger = LoggerFactory.getLogger(ShiroConfig.class);
//    LoggerFactory.getLogger(MyShiroConfig.class);

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManagers){
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        logger.warn("MyShiroConfig执行了");
        /**
         * 设置拦截规则 Map
         * anon 匿名可以访问
         * authc 认证可访问
         */
        Map map = new HashMap();
        map.put("/login.jsp","anon");
        map.put("/login1.jsp","anon");
        map.put("/admin/*","anon");
        map.put("/main.jsp","authc");
        map.put("/guru/*","authc");

        factoryBean.setFilterChainDefinitionMap(map);

        /**
         * 设置安全管理器
         */
        factoryBean.setSecurityManager(securityManagers);

        return factoryBean;
    }

    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(AuthorRealm myRealm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        /**
         * 设置自定义Realm,将自定义的Realm传进去就行了
         */
        defaultWebSecurityManager.setRealm(myRealm);

        return defaultWebSecurityManager;
    }

    @Bean
    public AuthorRealm myRealm(){
        return new AuthorRealm();
    }

}
